package com.zbcbms.controller;

import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.common.GetChartCommon;
import com.zbcbms.common.TableInitCommon;
import com.zbcbms.entity.tableData.BookTableData;
import com.zbcbms.service.impl.BookServiceImpl;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class QueryBookController implements Initializable {
    @FXML
    private TableView<BookTableData> searchTableView;
    @FXML
    private TableColumn<BookTableData, String> searchTableCode;
    @FXML
    private TableColumn<BookTableData, String> searchTableBookName;
    @FXML
    private TableColumn<BookTableData, String> searchTableAuthor;
    @FXML
    private TableColumn<BookTableData, String> searchTablePublishName;
    @FXML
    private TableColumn<BookTableData, String> searchTableYear;
    @FXML
    private TableColumn<BookTableData, String> searchTablePrice;
    @FXML
    private TableColumn<BookTableData, String> searchTableCategory;
    @FXML
    private TableColumn<BookTableData, String> searchTableStatus;
    @FXML
    private TableColumn operation;
    @FXML
    private PieChart pieChartBookType;
    @FXML
    private PieChart bookStatusPieChart;
    @FXML
    private TextField searchText;

    private GetChartCommon getChartCommon=new GetChartCommon();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
       bookTableInit(searchTableView,searchTableCode,searchTableBookName,searchTableStatus
                ,searchTableAuthor,searchTablePublishName,searchTableYear,searchTablePrice,searchTableCategory,operation);
        pieChartBookType.setData(getChartCommon.getBookTypeChartData());
        bookStatusPieChart.setData(getChartCommon.getBookStatusChartData());
    }

    public void bookTableInit(TableView<BookTableData> searchTableView,
                              TableColumn<BookTableData,String> searchTableCode,
                              TableColumn<BookTableData,String> searchTableBookName,
                              TableColumn<BookTableData,String> searchTableStatus,
                              TableColumn<BookTableData,String> searchTableAuthor,
                              TableColumn<BookTableData,String> searchTablePublishName,
                              TableColumn<BookTableData,String> searchTableYear,
                              TableColumn<BookTableData,String> searchTablePrice,
                              TableColumn<BookTableData,String> searchTableCategory,
                              TableColumn operation){
        searchTableCode.setCellValueFactory(cellData->cellData.getValue().getBookNumber());
        searchTableBookName.setCellValueFactory(cellData->cellData.getValue().getBookName());
        searchTableAuthor.setCellValueFactory(cellData->cellData.getValue().getAuthor());
        searchTablePublishName.setCellValueFactory(cellData->cellData.getValue().getPublishingHouse());
        searchTableYear.setCellValueFactory(cellData->cellData.getValue().getPublishingTime());
        searchTablePrice.setCellValueFactory(cellData->cellData.getValue().getPrice());
        searchTableCategory.setCellValueFactory(cellData->cellData.getValue().getType());
        searchTableStatus.setCellValueFactory(cellData->cellData.getValue().getStatus());
        operation.setCellFactory((col)->{
            TableCell<BookTableData,String> cell=new TableCell<BookTableData,String>(){
                public HBox addButton=new HBox();
                Label delBtn=new Label("删除");
                Label editBtn=new Label("编辑");
                Label empty = new Label("   ");
                {
                    addButton.getStylesheets().add("view/css/user.css");
                    addButton.setAlignment(Pos.CENTER);
                    delBtn.getStyleClass().addAll("delBtn","btn");
                    editBtn.getStyleClass().addAll("editBtn","btn");
                    addButton.getChildren().addAll(editBtn,empty,delBtn);
                }

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty){
                        delBtn.setOnMouseClicked((m)->{
                            BookTableData selectData=getTableView().getSelectionModel().getSelectedItem();
                            if (new AlertUtilCommon().AlterMessageOption(delBtn.getScene().getWindow(),"请你选择",
                                    "确认删除","取消删除","删除提示","是否删除该用户？")){
                                new BookServiceImpl().deleteById(selectData.getBookNumber().get());
                                initialize(null, null);
                            }
                        });
                        editBtn.setOnMouseClicked((m)->{
                            BookTableData selectData=getTableView().getSelectionModel().getSelectedItem();
                            AnchorPane anchorPane = (AnchorPane) getTableView().getParent().getParent().getParent();
                            HomePageController homePageController = new HomePageController();
                            homePageController.setMainFrameAnchorPane(anchorPane);
                            homePageController.do_toUpdateBook(selectData);
                        });
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        setGraphic(addButton);
                    }else {
                        setGraphic(null);
                    }
                }
            };
            return cell;
        });
        searchTableView.setItems(new BookServiceImpl().getBookTableViewData());
    }

    @FXML
    public void searchBtnAction(){
        String text = searchText.getText();
        new TableInitCommon().homeTableInit(searchTableView,searchTableCode,searchTableBookName,searchTableStatus,
                searchTableAuthor,searchTablePublishName,searchTableYear,searchTablePrice,searchTableCategory,
                new BookServiceImpl().getBookVoByKeyWord(text));
    }
}
